Imports System.Text

Public Class SUM_WaitStates
    Private Const Q As String = Chr(34)
    Private mlDatabaseID As Long
    Private msBaseURL As String = ""

    Private mbDataExists As Boolean = False

    Public Sub New( _
                ByVal sBaseURL As String, _
                ByVal lDatabaseID As Long)

        msBaseURL = sBaseURL
        mlDatabaseID = lDatabaseID
    End Sub

    Public ReadOnly Property DataExists() As Boolean
        Get
            Return mbDataExists
        End Get
    End Property

    Public Function Summerize() As String
        Dim oQueueFactory As New SQLAutoDocLib.ScanQueue_Factory

        Dim oDatabase As New SQLAutoDocLib.Database(mlDatabaseID)
        oDatabase.Load()

        '---Get data
        Dim oFactory As New SQLAutoDocLib.WaitStateLog_Factory
        Dim oData As DataTable = oFactory.WaitState10PointTrends(mlDatabaseID, DateAdd(DateInterval.Weekday, -7, Now), Now)


        Dim oSum As New StringBuilder

        With oSum
            .Append("<span style=" & Q & "font-size: 12px; font-weight: bold;" & Q & ">")
            .Append("Database WaitStates - Last 7 days")
            .Append("<br/>")
            .Append("</span>")

            .Append("<span style=" & Q & "font-size: 10px; font-weight: normal;" & Q & ">")
            .Append("Last scan: " & oQueueFactory.GetLastQueuedRequest(mlDatabaseID, SQLAutoDocLib.ScanSchedule.Type.WaitStateScan))
            .Append("<br/>")
            .Append("</span>")

            .Append("<table style=" & Q & "width: 200px;" & Q & ">")
            For Each oRow As DataRow In oData.Rows
                Dim sAvgWaitType_URL As String = MakeChartString_AvgWaitTime(oRow)

                If sAvgWaitType_URL > "" Then

                    .Append("<tr>")
                    .Append("<td>")
                    .Append("<a href=" & Q & msBaseURL & "/SAD/SQWaitStatesL.aspx?D=" & mlDatabaseID & "&T=" & oRow.Item("Wait_Type") & Q & " title=" & Q & oRow.Item("Desc") & Q & ">")
                    .Append(oRow.Item("Wait_Type"))
                    .Append("</a>")
                    .Append("</td>")

                    .Append("<td>")
                    .Append("<img src=" & Q & sAvgWaitType_URL & Q & " border=" & Q & "1" & Q & "/>")
                    .Append("<br/>")
                    .Append(oRow.Item("P10_AvgRequests"))
                    .Append("</td>")
                    .Append("</tr>")

                End If
            Next
            .Append("</table>")
        End With

        Return oSum.ToString
    End Function

    Private Function MakeChartString_AvgWaitTime(ByVal oRow As DataRow) As String
        Dim oURL As New StringBuilder
        Dim iMaxValue As Integer = 0

        oURL.Append("http://" & msBaseURL & "/SAD/ilc.aspx?T=L&W=60&H=20&TH=2&FG=5959FF&BG=FFFFFF")

        For i As Integer = 1 To 10
            oURL.Append("&DP=" & oRow.Item("P" & i & "_AvgWaitTime"))

            If IsNumeric(oRow.Item("P" & i & "_AvgSignalTime")) _
                AndAlso oRow.Item("P" & i & "_AvgSignalTime") > iMaxValue Then

                iMaxValue = oRow.Item("P" & i & "_AvgSignalTime")
            End If
        Next

        If iMaxValue > 0 Then
            Return oURL.ToString
        Else
            Return ""
        End If
    End Function
End Class
